<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:06:14
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceChargeRepository;
use App\Repositories\FinanceVoucherRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property FinanceChargeRepository $repository
 * @property FinanceVoucherRepository $voucher
 */
class ChargeService extends Service
    {
    public $state = 2;
    public function boot(FinanceChargeRepository $repository, FinanceVoucherRepository $voucher)
        {
        $this->repository = $repository->where("state", $this->state);
        $this->voucher    = $voucher;
        }


    public function list()
        {
        $data = $this->repository->condition()->getGrid();
        return $data;
        }
    protected function save()
        {
        $gdata = request();
        if (is_numeric($gdata['id']) && $gdata['id'] > 0) {
            $ddata = $this->repository->whereId($gdata['id'])->first();
            if ($ddata->status_id == 1) {
                fail("其他支出:[{$ddata->code}]已审核！不能编辑！");
                }
            }

        $gdata          = $gdata->toArray();
        $gdata['state'] = $this->state;
        $gdata          = $this->repository->autosave($gdata);

        return $gdata;
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id == 1) {
                fail("其他支出:[{$gdata->code}]已审核！不能审核！");
                }
            auditSave($gdata);
            $gdata->finance();
            return $gdata;
            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id == 0) {
                fail("其他支出:[{$gdata->code}]未审核！不能反审！");
                }
            $gdata->nofinance();
            auditSave($gdata, false);
            return $gdata;
            });
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id == 1) {
                fail("单据已审核！禁止删除");
                }
            return $v->delete();
            });
        }
    }